package cn.aib.eduservice.service.impl;

import cn.aib.eduservice.entity.EduTeacher;
import cn.aib.eduservice.mapper.EduTeacherMapper;
import cn.aib.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-11-14
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    @Override
    @Cacheable(value = "eduservice",key = "'selectTeacherlist'")
    public List<EduTeacher> selectTeacherlist(QueryWrapper<EduTeacher> teacherWrapper) {
        List<EduTeacher> eduTeachers = baseMapper.selectList(teacherWrapper);

        return eduTeachers;
    }

    @Override
    @CacheEvict(value = "eduservice",key = "'selectTeacherlist'")
    public boolean removeTeacherById(String id) {
        int i = baseMapper.deleteById(id);
        System.out.println("removeTeacherById--------------------"+i);
        return i>0?true:false;
    }


    @Override
    @CacheEvict(value = "eduservice",key = "'selectTeacherlist'")
    public boolean saveTeacher(EduTeacher teacher) {
        int i = baseMapper.insert(teacher);
        return i>0?true:false;
    }

    @Override
    @CacheEvict(value = "eduservice",key = "'selectTeacherlist'")
    public boolean updateTeacherById(EduTeacher teacher) {
        int i = baseMapper.updateById(teacher);

        return i>0?true:false;
    }

    @Override
    public Map<String, Object> selectTeacherFrontList(long currentPage, long limit) {
        //创建page对象
        Page<EduTeacher> page = new Page<>(currentPage, limit);
        //设置条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();

        wrapper.orderByDesc("id");
        //查询
        baseMapper.selectPage(page, wrapper);

        //设置到map
        List<EduTeacher> records = page.getRecords();
        long current = page.getCurrent();
        long pages = page.getPages();
        long size = page.getSize();
        long total = page.getTotal();
        boolean hasNext = page.hasNext();
        boolean hasPrevious = page.hasPrevious();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("teachers", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        //返回
        return map;
    }
}
